package cn.com.winning.ecare.activity;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import cn.com.winning.ecare.common.BtoECalendarDialog;
import cn.com.winning.ecare.common.Grideviewmenu;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.utils.DisplayUtil;
import cn.com.winning.ecare.utils.SlidingLayout;
import cn.com.winning.ecare.widgets.CustomProgressDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.OnTabChangeListener;

/**暂停用
 * @author Administrator
 *
 */
public class Costmain extends TabActivity
{

	TabHost tabHost;
	private TabWidget mTabWidget;

	/*
	 * 注意： 对于TabHost、布局文件中必须包含 TabHost、TabWidget 、FrameLayout
	 * 如果继承TabActivity，并且通过getTabHost()方法来获取TabHost
	 * 那么三者的ID必须是android.R.id.tabhost、android.R.id.tabs、android.R.id.tabcontent
	 * 如果继承Activity，可以通过findViewById来获取这三个组件，此时ID可自定义
	 */
	/**
	 * 侧滑布局对象，用于通过手指滑动将左侧的菜单布局进行显示或隐藏。
	 */
	// private SlidingLayout slidingLayout;

	private LinearLayout content;

	/**
	 * menu按钮，点击按钮展示左侧布局，再点击一次隐藏左侧布局。
	 */
	private ImageButton menuButton;

	private GridView gridView;

	private Costmain othis = this;
	Grideviewmenu gm;
	CustomProgressDialog proDialog;// 加载框
	private ImageButton btnSearch;// 日期选择按钮
	private LinearLayout layout;
	private int height, statusBarHeight;
	private String startDate = "", endDate = "";
	MyApplication myApplication;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.cost_main_first);
		myApplication = (MyApplication) getApplication();
		myApplication.addActivity(this);
		// 获得TabHost
		// slidingLayout = (SlidingLayout) findViewById(R.id.slidingLayout);
		menuButton = (ImageButton) findViewById(R.id.menuButton);
		// content = (LinearLayout) findViewById(R.id.content);
		tabHost = getTabHost();
		mTabWidget = (TabWidget) this.findViewById(android.R.id.tabs);
		// 加载日期控件
		btnSearch = (ImageButton) this.findViewById(R.id.fysearchButton);
		layout = (LinearLayout) this.findViewById(R.id.fylayout);

		// 接受参数，主要用于日期选择后的日期获取，然后再传入每个tab
		Intent intent = getIntent();
		String startDate1 = intent.getStringExtra("startDate");
		String endDate1 = intent.getStringExtra("endDate");
		int tab = 0;// 记录是哪个tab，默认是一日清单为0，只有费用查询有日期选择，故在费用查询页点击查询后还是停留在费用查询tab改为1
		if ((startDate1 == null || "".equals(startDate1)) && (endDate1 == null || "".equals(endDate1)))
		{
			// 数据少方便演示，暂默认全部数据，开始日期为空
			// //初次打开，没有选择日期
			// Date now = new Date();
			// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			// startDate = simpleDateFormat.format(now);//默认当天
		}
		else
		{
			// 选择的日期赋值
			startDate = startDate1;
			endDate = endDate1;
			tab = 1;
			btnSearch.setVisibility(View.VISIBLE);// 费用查询显示出查询按钮
		}

		// ////////////////处理tabhost加载选定页面，始终先加载第一个再跳入选定页面问题
		Field mCurrentTab = null;
		try
		{
			mCurrentTab = tabHost.getClass().getDeclaredField("mCurrentTab");
			mCurrentTab.setAccessible(true);
			mCurrentTab.setInt(tabHost, -2);
		}
		catch (SecurityException e)
		{
			e.printStackTrace();
		}
		catch (IllegalArgumentException e)
		{
			e.printStackTrace();
		}
		catch (NoSuchFieldException e)
		{
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		// //////////////////////////////////////

		Intent intent1 = new Intent();
		intent1.putExtra("startDate", startDate);
		intent1.putExtra("endDate", endDate);
		intent1.setClass(this, CostSelect.class);

		Intent intent2 = new Intent();
		intent2.setClass(this, Costeveryday.class);

		// 新建一个tab并设置它的，Tag,标题，图标，内容
		tabHost.addTab(tabHost.newTabSpec("一日清单").setIndicator("一日清单").setContent(intent2));
		tabHost.addTab(tabHost.newTabSpec("医疗费用").setIndicator("医疗费用").setContent(intent1));
		// //////////////
		try
		{
			if (tab == 0)
			{
				mCurrentTab.setInt(tabHost, 1);
			}
			else
			{
				mCurrentTab.setInt(tabHost, 0);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		// ///////////////////////
		tabHost.setCurrentTab(tab);// 设置初始选中状态为第一个tab

		// 将监听滑动事件绑定在contentListView上
		// slidingLayout.setScrollEvent(content);
		menuButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				// if (slidingLayout.isLeftLayoutVisible()) {
				// slidingLayout.scrollToRightLayout();
				// } else {
				// slidingLayout.scrollToLeftLayout();
				// }
				othis.onBackPressed();
			}
		});
		iniTabHost();

		// gm = new Grideviewmenu();
		// gridView = (GridView) findViewById(R.id.gridview_menu);
		// // 生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
		// SimpleAdapter adapter = new SimpleAdapter(this, getData(), // 数据来源
		// R.layout.grideview_menulist,// XML实现
		// new String[] { "ItemImage", "ItemText" }, // 动态数组与ImageItem对应的子项
		// new int[] { R.id.gridviewmenu_imageview, R.id.gridviewmenu_textview }//
		// //ImageItem的XML文件里面的一个ImageView,两个TextView
		// // ID
		// );
		// // 添加并且显示
		// gridView.setAdapter(adapter);
		// // 添加消息处理
		// gridView.setOnItemClickListener(clickListener);
		// TextView nametv = (TextView)findViewById(R.id.username_menu);
		// MyApplication myapp=(MyApplication)getApplication();
		// nametv.setText(myapp.getHzxm());
		// TextView tv = (TextView)findViewById(R.id.out_menu);
		// tv.setOnClickListener(new OnClickListener() {
		// @Override
		// public void onClick(View v) {
		// finish();
		// Intent intent = new Intent();
		// intent.setClass(othis, Login.class);
		// startActivity(intent);
		// }
		// });

		setListener();
	}

	private void iniTabHost()
	{
		// 初始化TabHost
		for (int i = 0; i < mTabWidget.getChildCount(); i++)
		{
			RelativeLayout view = (RelativeLayout) mTabWidget.getChildAt(i);

			TextView text = (TextView) view.findViewById(android.R.id.title);
			text.setTextSize(18);
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text.getLayoutParams();
			params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
			params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
			text.setLayoutParams(params);
			text.setGravity(Gravity.CENTER);
			view.setBackgroundColor(getResources().getColor(R.color.color_white));
			if (tabHost.getCurrentTab() == i)
			{
				text.setTextColor(getResources().getColor(R.color.listheadtext_color));

			}
			else
			{
				text.setTextColor(getResources().getColor(R.color.test_color_register));
			}
			int heightPx = DisplayUtil.dip2px(this, 50);
			mTabWidget.getChildAt(i).getLayoutParams().height = heightPx;
		}
		// TabHost切换
		tabHost.setOnTabChangedListener(new OnTabChangeListener()
		{

			public void onTabChanged(String tabId)
			{
				// TODO Auto-generated method stub
				for (int i = 0; i < mTabWidget.getChildCount(); i++)
				{
					View view = mTabWidget.getChildAt(i);
					TextView text = (TextView) view.findViewById(android.R.id.title);

					if (tabHost.getCurrentTab() == i)
					{
						if (i == 1)
						{
							btnSearch.setVisibility(View.VISIBLE);// 费用查询显示出查询按钮
						}
						else
						{
							btnSearch.setVisibility(View.GONE);
						}
						text.setTextColor(getResources().getColor(R.color.listheadtext_color));
					}
					else
					{
						text.setTextColor(getResources().getColor(R.color.test_color_register));
					}
				}
			}

		});
	}

	private void setListener()
	{
		btnSearch.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				// 初始化一个自定义的Dialog
				final BtoECalendarDialog dialog = new BtoECalendarDialog(Costmain.this, R.style.MyDialog,startDate,endDate);
				// 获取状态栏高度
				Rect frame = new Rect();
				getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
				statusBarHeight = frame.top;
				Log.i("aaa", "statusBarHeight :" + statusBarHeight);
				// 获取layout高度
				height = layout.getHeight();
				dialog.show();
				Window mWindow = dialog.getWindow();
				WindowManager windowManager = getWindowManager();
				Display display = windowManager.getDefaultDisplay();
				WindowManager.LayoutParams lp = mWindow.getAttributes();
				lp.x = 0;
				lp.y = height + statusBarHeight;
				lp.width = (int) (display.getWidth()); // 设置宽度
				lp.height = (int) (display.getHeight()) - height - statusBarHeight;// 设置高度
				mWindow.setAttributes(lp);

				Button btnok = (Button) dialog.findViewById(R.id.btnok);
				Button btncancel = (Button) dialog.findViewById(R.id.btncancel);

				btnok.setOnClickListener(new View.OnClickListener()
				{

					@Override
					public void onClick(View v)
					{
						// TODO Auto-generated method stub
						// 选择开始日期
						TextView startDate = (TextView) dialog.findViewById(R.id.tvSelectedstart);
						// 选择结束日期
						TextView endDate = (TextView) dialog.findViewById(R.id.tvSelectedend);
						String sd = startDate.getText().toString();
						String ed = endDate.getText().toString();

						dialog.dismiss();
						proDialog = CustomProgressDialog.createDialog(othis);
						proDialog.setMessage("数据加载中...");
						proDialog.show();
						proDialog.setCanceledOnTouchOutside(false);

						// 传入选择的日期，重新打开Costmain(要同时给tabhost中的两个tab页传入参数，暂无好方法，暂重刷主页面，同时传参)
						Intent intent = getIntent();
						intent.putExtra("startDate", sd);
						intent.putExtra("endDate", ed);
						intent.setClass(othis, Costmain.class);
						startActivity(intent);
						othis.finish();

					}
				});

				btncancel.setOnClickListener(new View.OnClickListener()
				{

					@Override
					public void onClick(View v)
					{
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});

			}
		});

	}

	// 获取GrideView数据
	// private ArrayList<HashMap<String, Object>> getData() {
	// MyApplication myApplication = (MyApplication) getApplication();
	// return gm.initItem(myApplication);//初始化GrideView
	// }
	// // 当AdapterView被单击(触摸屏或者键盘)，则返回的Item单击事件
	// private OnItemClickListener clickListener = new OnItemClickListener() {
	//
	// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
	// long arg3) {
	// if (slidingLayout.isLeftLayoutVisible()) {
	// Intent intent = null;
	// HashMap<String, Object> Item = (HashMap<String, Object>) arg0
	// .getItemAtPosition(arg2);
	// intent = gm.findActivity(intent,Item, othis);
	// startActivity(intent);
	// othis.finish();
	// }
	// }
	// };
}
